﻿using System;
using System.Collections.Generic;

namespace DictionaryExtension
{
    public static class StringExtension
    {
        public static Dictionary<string, string> ToDictionary(this String InputString, string Seperator)
        {
            string[] ValuePairs = SplitStringBySeperator(InputString, Seperator);
            return BuildDictionaryByValuePairs(ValuePairs);
        }

        public static string[] SplitStringBySeperator(string InputString, string Seperator)
        {
            return InputString.Split(new string[] { Seperator }, StringSplitOptions.None);
        }

        public static string[] ResolveValuePair(string ValuePair)
        {
            return ValuePair.Split(new string[] {"="}, StringSplitOptions.None);
        }

        public static Dictionary<string, string> BuildDictionaryByValuePairs(string[] ValuePairStrings)
        {
            Dictionary<string, string> ReturnValue = new Dictionary<string, string>();

            string[] CurrentValuePair;

            foreach (string ValuePairString in ValuePairStrings)
            {
                CurrentValuePair = ResolveValuePair(ValuePairString);
                ReturnValue.Add(CurrentValuePair[0], CurrentValuePair[1]);
            }

            return ReturnValue;
        }
    }
}